package com.jjlin.chapter_2;

import java.util.*;
public class CD114{
    public static class Node{
        public int val;
        public Node next;
        public Node(int data){
            this.val = data;
        }
    }

    public static Node createNodeList(int[] nums){
        if(nums == null || nums.length < 1)
            return null;
        Node head = new Node(nums[0]);
        Node pre = head;
        for(int i = 1; i < nums.length; i++){
            Node cur = new Node(nums[i]);
            pre.next = cur;
            pre = cur;
        }
        return head;
    }

    public static void printNodeList(Node head){
        Node cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //解法1：利用栈的特性
    public static Node addList(Node head1, Node head2){
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        while(head1 != null){
            stack1.push(head1.val);
            head1 = head1.next;
        }
        while(head2 != null){
            stack2.push(head2.val);
            head2 = head2.next;
        }
        int ca = 0;//进位
        int sum = 0;//总和
        int n1 = 0;
        int n2 = 0;
        Node head = null;
        while(!stack1.isEmpty() || !stack2.isEmpty()){
            n1 = !stack1.isEmpty() ? stack1.pop() : 0;
            n2 = !stack2.isEmpty() ? stack2.pop() : 0;
            sum = n1 + n2 + ca;
            ca = sum / 10;
            Node node = new Node(sum % 10);
            node.next = head;
            head = node;
        }
        if(ca == 1){
            Node node = new Node(1);
            node.next = head;
            head = node;
        }
        return head;
    }

    //解法2：反转链表
    public static Node addList1(Node head1, Node head2){
        head1 = reverseList(head1);
        head2 = reverseList(head2);
        Node cur1 = head1, cur2 = head2;
        int ca = 0;//进位
        int sum = 0;//总和
        int n1 = 0;
        int n2 = 0;
        Node head = null;
        while(cur1 != null || cur2 != null){
            n1 = cur1 != null ? cur1.val : 0;
            n2 = cur2 != null ? cur2.val : 0;
            sum = n1 + n2 + ca;
            ca = sum / 10;
            Node node = new Node(sum % 10);
            node.next = head;
            head = node;
            cur1 = cur1 != null ? cur1.next : cur1;
            cur2 = cur2 != null ? cur2.next : cur2;
        }
        if(ca == 1){
            Node node = new Node(1);
            node.next = head;
            head = node;
        }
        head1 = reverseList(head1);
        head2 = reverseList(head2);
        return head;
    }

    //反转链表
    public static Node reverseList(Node head){
        Node pre = null;
        Node next = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] nums = new int[n];
        for(int i = 0; i < n; i++)
            nums[i] = sc.nextInt();
        Node head1 = createNodeList(nums);
        nums = new int[m];
        for(int i = 0; i < m; i++)
            nums[i] = sc.nextInt();
        Node head2 = createNodeList(nums);
        Node head = addList(head1, head2);
        printNodeList(head);
    }

}
